package com.zhixinsou.wuyang.entry;

import com.alibaba.fastjson.JSON;
import com.zhixinsou.wuyang.model.AnalysisReport;
import com.zhixinsou.wuyang.model.SheetModel;
import com.zhixinsou.wuyang.process.*;
import com.zhixinsou.wuyang.util.FileUtil;
import com.zhixinsou.wuyang.util.StringUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Entry {
    public static void main(String[] args) {
        String filePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\The Benchmark Company\\Benchmark_2008_File 7.pdf";
//        String filePath = "D:\\Dropbox\\All downloaded reports\\JEFFERIES\\JEFFERIES_2005_File 4.pdf";
//        String filePath = "D:\\Dropbox\\All downloaded reports\\JEFFERIES\\JEFFERIES_2006_File 1.pdf";
//        String filePath = "D:\\Dropbox\\All downloaded reports\\UBS Research\\UBS Research_2005_file 24.pdf";
//        String filePath = "D:\\Dropbox\\All downloaded reports\\JPMorgan\\JPMORGAN_2004_file 2.pdf";
//        String filePath = "D:\\Dropbox\\All downloaded reports\\With checkbox\\Buckingham\\Buckingham 2004\\Buckingham-2004-page9.pdf";

//        ProcessJEFFERIES.process("D:\\Dropbox\\All downloaded reports\\JEFFERIES", 
//                "data/result/JEFFERIESResult.xls");

//        String ThinkEquityFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\Thinkequity LLC\\";
//        ThinkEquityFilePath = "data/test";
//        ProcessThinkEquity.process(ThinkEquityFilePath,"data/result/ThinkEquityResult.xls");
//        ProcessThinkEquity.process(ThinkEquityFilePath,"data/result/ThinkEquityTest.xls");

//        String BBTFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\BB_T CAPITAL MARKETS\\";
//        BBTFilePath = "data/test";
//        ProcessBBT.process(BBTFilePath,"data/result/BBTResult.xls");
//        ProcessBBT.process(BBTFilePath,"data/result/BBTTest.xls");

        String BenchmarkFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\The Benchmark Company\\";
//        BenchmarkFilePath = "data/test";
        ProcessBenchmark.process(BenchmarkFilePath,"data/result/BenchmarkResult.xls");
//        ProcessBenchmark.process(BenchmarkFilePath,"data/result/BenchmarkTest.xls");

//        String FeltlFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\Feltl & Company\\";
//        FeltlFilePath = "data/test";
//        ProcessFeltl.process(FeltlFilePath,"data/result/FeltlResult.xls");
//        ProcessFeltl.process(FeltlFilePath,"data/result/FeltlTest.xls");

//        String JPMorganFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\JPMorgan";
//        String JPMorganFilePath = "data/test";
//        ProcessJPMorgan.process(JPMorganFilePath, "data/result/JPMorganFinalResult.xls");

//        String OppenheimerFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\OPPENHEIMER AND CO\\";
//        OppenheimerFilePath = "data/test";
//        ProcessOPPENHEIMER.process(OppenheimerFilePath,"data/result/OppenheimerResult.xls");
//        ProcessOPPENHEIMER.process(OppenheimerFilePath,"data/result/Oppenheimer18.xls");


//        String RBCFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\RBC CM\\";
//        RBCFilePath = "data/test";
//        ProcessRBC.process(RBCFilePath,"data/result/RBCResult.xls");
//        ProcessRBC.process(RBCFilePath,"data/result/RBCTest.xls");

//        String PrudentialFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\Prudential Equity Group, Inc\\";
//        PrudentialFilePath = "data/test";
//        ProcessPrudential.process(PrudentialFilePath,"data/result/PrudentialResult.xls");
//        ProcessPrudential.process(PrudentialFilePath,"data/result/PrudentialTest.xls");

//        String PacificFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\Pacific Growth\\";
//        PacificFilePath = "data/test";
//        ProcessPacific.process(PacificFilePath,"data/result/PacificResult.xls");
//        ProcessPacific.process(PacificFilePath,"data/result/PacificTest.xls");

//        String NatixisFilePath = "C:\\Users\\lwh\\Dropbox\\All downloaded reports\\Natixis Bleichroeder\\";
//        NatixisFilePath = "data/test";
//        ProcessNatixis.process(NatixisFilePath,"data/result/NatixisResult.xls");
//        ProcessNatixis.process(NatixisFilePath,"data/result/NatixisTest.xls");

//        String BuckinghamFilePath = "D:\\Dropbox\\All downloaded reports\\With checkbox\\Buckingham\\Buckingham 2003";
//        ProcessBuckingham.process3(BuckinghamFilePath, "data/result/Buckingham2003.xls");

//        String BuckinghamFilePath = "D:\\Dropbox\\All downloaded reports\\With checkbox\\Buckingham\\Buckingham 2004";
//        ProcessBuckingham.process4(BuckinghamFilePath, "data/result/Buckingham2004.xls");
        
//        String content = FileUtil.pdfReaderWithFonts(filePath, 11, 11);
//        String content = FileUtil.pdfReaderWithFontsAndColor(filePath, 6, 6);
//        String content = FileUtil.pdfReaderWithFonts(filePath, 117, 117);
//        String content = FileUtil.pdfReader(filePath, 10,10,false);
//        String[] lines = content.split("\r\n");
//        for (int i = 0; i < lines.length; i++) {
//            String line = lines[i].trim();
//            System.out.println(line);
//        }
//        Pattern patternTicker = Pattern.compile("(Arial,Bold\\]<17\\.0>|ArialMT\\-Bold\\]<17\\.0>).*\\(([A-Z]+)\\)");
//        for (int i = 0; i < lines.length; i++) {
//            String line = lines[i];
//            Matcher matcherTicker = patternTicker.matcher(line);
//            if (matcherTicker.find()) {
//                System.out.println("The ticker line is :" + lines[i]);
//                System.out.println("Ticker: " + matcherTicker.group(2));
//                Pattern patternRating = Pattern.compile("(Arial,Bold\\]<12\\.0>|ArialMT\\-Bold\\]<12\\.0>)(.*)");
//                for(int j = i + 1; j < lines.length; j++){
//                    Matcher matcherRating = patternRating.matcher(lines[j]);
//                    if(matcherRating.find()){
//                        String orginRating = matcherRating.group(2);
//                        System.out.println("The rating line is " + orginRating);
//                        if(orginRating.contains("[")){
//                            int end = orginRating.indexOf("[");
//                            System.out.println("Rating: " + orginRating.substring(0,end));
//                        }
//                        else{
//                            System.out.println("Rating: " + orginRating);
//                        }
//                        break;
//                    }
//                }
//                break;
//            }
//        }
//        Pattern patternCompany = Pattern.compile("(Verdana-Bold\\]<9\\.0>)(.*)");
//        for (int i = 0; i < lines.length; i++) {
//            String line = lines[i];
//            Matcher matcherCompany = patternCompany.matcher(line);
//            if (matcherCompany.find()) {
//                System.out.println(line);
//                String originCompany = matcherCompany.group(2);
//                System.out.println("Set company name here111" + originCompany);
//                if(originCompany.contains("[")){
//                    int end = originCompany.indexOf("[");
//                    System.out.println(originCompany.substring(0,end));
//                }
//                else{
//                    System.out.println(originCompany);
//                }
//                break;
//            }
//        }

//        AnalysisReport report = new AnalysisReport();
//        report.setFilePath(filePath);
//        report.setStartPage(95);
//        report.setEndPage(95);
//        ProcessBenchmark.getAnalysisReportDetailVersion2(report);

//        Pattern patternTicker = Pattern.compile("CONTENTS:\\s(.*)");
//        for (int i = 0; i < lines.length; i++) {
//            String line = lines[i];
//            Matcher matcherTicker = patternTicker.matcher(line);
//            if (matcherTicker.find()) {
//                String value = matcherTicker.group(1);
//                System.out.println(value.split(",")[0]);
//                break;
//            }
//        }

//        Pattern patternCompany = Pattern.compile("\\[\\S*>[A-Za-z]+");
//        String company = "";
//        for (int i = 0; i < lines.length; i++) {
//            String line = lines[i];
//            if(line.contains("Natexis Bleichroeder Inc.")){
//                for(int j = i + 1; j < lines.length; j++){
//                    Matcher matcherCompany = patternCompany.matcher(lines[j]);
//                    if(matcherCompany.find()){
//                        String originCompany = lines[j];
//                        System.out.println("Origin company: " + originCompany);
//                        if(originCompany.contains(">")){
//                            int begin = originCompany.indexOf(">");
//                            company = originCompany.substring(begin + 1);
//                        }
//                        else{
//                            if(!lines[j + 1].startsWith("[")){
//                                String another = lines[j + 1];
//                                if(another.contains("[")){
//                                    int end = another.indexOf("[");
//                                    another = another.substring(0,end);
//                                }
//                                company = originCompany + " " + another;
//                            }
//                            else {
//                                company = originCompany;
//                            }
//                        }
//                        System.out.println(company);
//                        break;
//                    }
//                }
//            }
//        }


//        String test = "[IPDFDN+Symbol]<10.0>• [Times-Bold]<7.0>Amol A Manjrekar[Times-Roman]<10.0>Our FY05 estimates are based on 13.0%, 14.0%, 17.0%, and 14.0% enrollment (1-212) 622-6518";
//        test = test.substring(test.indexOf("[Times-Bold]<7.0>") + 17);
//        System.out.println(test.substring(0,test.indexOf("[")));

//        String test = "[Times-Italic]<7.0>Ticker MON EPS [Times-Bold]<7.0>2003E 2004E 2005E";
//        int begin = test.indexOf(">");
//        int end = test.lastIndexOf("[");
//        System.out.println(test.substring(begin+1,end));

//        AnalysisReport report = new AnalysisReport();
//        report.setFilePath("D:\\Dropbox\\All downloaded reports\\With checkbox\\Buckingham\\Buckingham 2003\\Buckingham-2003-page1.pdf");
//        report.setStartPage(10);
//        report.setEndPage(10);
//        ProcessBuckingham.getAnalysisReportDetail2004_2012_new(report);
        
//
//        String[] linesWithFonts = content.split("\r\n");
//        boolean authorStart = false;
//        boolean authorFound = false;
//        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9]+");
//        Pattern patternName = Pattern.compile("\\[ArialNarrow,Bold\\]([^\\[]+)\\[ArialNarrow\\]");
//        for(int last = linesWithFonts.length - 1;last > -1;last--) {
//            String line = linesWithFonts[last].trim();
//            Matcher matcher = patternEmail.matcher(line);
//            if (matcher.find()) {
//                authorStart = true;
//                continue;
//            }
//
//            if (authorStart) {
//                Matcher matcherName = patternName.matcher(line);
//                while (matcherName.find()) {
//                    System.out.println(matcherName.group(1));
//                    authorFound = true;
//                }
//                
//                if(authorFound)
//                    break;
//            }
//        }
        
//        Pattern patternSentence = Pattern.compile("([0-9a-z]+)\\.\\s*[^\\.]+?covering this company[^\\.]+?\\.");
//        Pattern patternTable = Pattern.compile("Company Name\\s*Reuters");
//        Matcher matcher = patternSentence.matcher(content);
//        boolean tableStart = false;
//        while (matcher.find()) {
//            String companyIndex = matcher.group(1);
//            String sentence = matcher.group().replace("\r\n", "");
//            System.out.println(companyIndex + " --> " + sentence);
//
//            for (String line : lines) {
//                Matcher matcherTable = patternTable.matcher(line.trim());
//                if (!tableStart && matcherTable.find()) {
//                    tableStart = true;
//                    continue;
//                }
//
//                if (tableStart) {
//                    Pattern pattern = Pattern.compile("([a-zA-Z0-9]+).*" + companyIndex + "[^a-zA-Z0-9]+.*");
//                    Matcher m = pattern.matcher(line);
//                    if (m.find()) {
//                        System.out.println("Company name : " + m.group());
//                    }
//                }
//                
//                if(tableStart && line.contains("Source:"))
//                    break;
//            }
//        }
        
//        System.out.println("\n----------------------");
//        boolean authorStart = false;
//        Pattern patternEmail = Pattern.compile("@[a-zA-Z0-9]+");
//        Pattern patternName = Pattern.compile("\\[ArialNarrow,Bold\\]([^\\[]+)\\[ArialNarrow\\]");
//        for(int last = lines.length - 1;last > -1;last--) {
//            String line = lines[last].trim();
//            Matcher matcher = patternEmail.matcher(line);
//            if (matcher.find()) {
//                authorStart = true;
//                continue;
//            }
//
//            if (authorStart) {
//                Matcher matcherName = patternName.matcher(line);
//                while (matcherName.find()) {
//                    System.out.println(matcherName.group(1));
//                }
//                break;
//            }
//        }
        
//
//        int last = lines.length - 1;
//        Pattern patternName = Pattern.compile("^([^,]+)[^@]+@");
//        boolean isEmpty = true;
//        for(; last > -1; last--) {
//            boolean find = false;
//            Matcher matcher = patternName.matcher(lines[last].trim());
//            while (matcher.find()) {
//                System.out.println(matcher.group(1));
//                find = true;
//                isEmpty = false;
//            }
//            if(!find && !isEmpty)
//                break;
//        }
        
//        String filePath = "data/pdf/JEFFERIES_2003_File 1.pdf";
//        List<AnalysisReport> reports = ProcessJEFFERIES.getAnalysisReportPageInfo(filePath);
//        System.out.println("Total : " + reports.size());
//        for (AnalysisReport report : reports) {
//            System.out.println(JSON.toJSON(report));
//        }

//        List<AnalysisReport> reports = ProcessJEFFERIES.getAnalysisReportPageInfo(filePath);
//        for (AnalysisReport report : reports) {
//            ProcessJEFFERIES.getAnalysisReportDetail2003_2005(report);
//            System.out.println(JSON.toJSON(report));
//        }
//        ProcessJEFFERIES.getAnalysisReportDetail2003_2005(reports.get(1));
//        System.out.println(JSON.toJSON(reports.get(1)));
//        System.out.println("Success!");

//        List<AnalysisReport> reports = new ArrayList<>();
//        AnalysisReport t = new AnalysisReport();
//        t.setTicker("1");
//        t.setDate("123");
//        t.setOwnershipDisclosures(new ArrayList<>());
//        reports.add(t);
        
//        List<String> headers = new ArrayList<>();
//        headers.add("Date");
//        headers.add("Analyst name");
//        headers.add("Company name");
//        headers.add("Ticker");
//        headers.add("Rating");
//        headers.add("Sentences");
//        headers.add("StartPage");
//        headers.add("EndPage");
//        headers.add("FilePath");
//        SheetModel sheetModel = FileUtil.makeSheet(resultPath, "result", headers);
//
//        List<String[]> data = new ArrayList<>();
//        List<AnalysisReport> reports = ProcessJEFFERIES.getAnalysisReportPageInfo(filePath);
//        for (AnalysisReport report : reports) {
//            ProcessJEFFERIES.getAnalysisReportDetail2003_2005(report);
//            String sentence;
//            if(report.getOwnershipDisclosures().size() == 1)
//                sentence = report.getOwnershipDisclosures().get(0);
//            else
//                sentence = JSON.toJSONString(report.getOwnershipDisclosures());
//
//            String[] line = new String[]{report.getDate(), JSON.toJSONString(report.getAnalystNames()), report.getCompanyName(), report.getTicker(), 
//                    report.getRating(), sentence, report.getStartPage() + "", report.getEndPage() + "", report.getFilePath()};
//            data.add(line);
//        }
//
//        FileUtil.exportSheet(sheetModel.getSheet(), data);
//        FileUtil.closeSheet(sheetModel);
//        System.out.println("Success!");
        
    }
}

